#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from shutil import copyfile

ROOT = '/home/dell/'
# generator
dataset_name = "Dresden"
input_path = os.path.join(ROOT, "dataset", dataset_name)

class Download():
    def __init__(self, name="Dresden"):
        self._name = name
        self._dataset_root = os.path.join(ROOT, "dataset")
        self._dataset_path = os.path.join(self._dataset_root, self._name)

        self.url_path = os.path.join(self._name + ".txt")
        if not os.path.exists(self.url_path):
            raise Exception("Dataset url file `{:s}` not exists!".format(self.url_path))
        self.rebuild()

    def rebuild(self):
        try:
            if not os.path.exists(self._dataset_path):
                os.mkdir(self._dataset_path, 0o755)
        except Exception as e:
            print("=> error:{:s}".format(str(e)))

    def build_path(self, brand, model, device):
        try:
            brand_path = os.path.join(self._dataset_path, brand)
            model_path = os.path.join(brand_path, model)
            device_path = os.path.join(model_path, device)
            if not os.path.exists(brand_path):
                os.mkdir(brand_path, 0o755)
            if not os.path.exists(model_path):
                os.mkdir(model_path, 0o755)
            if not os.path.exists(device_path):
                os.mkdir(device_path, 0o755)
        except Exception as e:
            print("=> error:{:s}".format(str(e)))

    def download(self, url, factory, model, device, name):
        copyfile(url, os.path.join(self._dataset_path, factory, model, device, name))
        return True

    def run(self, debug=False):
        errors = []
        with open(self.url_path, "r+") as fp:
            while True:
                line = fp.readline().rstrip("\n")
                name = line.split("/")[-1]
                if name == '':
                    return
                brand, model, device = name.split("_")[0], name.split("_")[1], name.split("_")[2]
                print(brand)
                print(model)
                print(device)
                self.build_path(brand, model, device)
                result = self.download(line, brand, model, device, name)

                if debug: print(line)
                if not result: errors.append(line)
                if not line: break
        return errors


def listfiles(rootDir, txtfile):
    ftxtfile = open(txtfile, 'w')
    list_dirs = os.walk(rootDir)
    count = 0
    dircount = 0
    for root,dirs,files in list_dirs:
        for d in dirs:
            print(os.path.join(root, d))
            dircount += 1
        for f in files:
             print(os.path.join(root, f))
             ftxtfile.write(os.path.join(root, f) + '\n')
             count += 1
    print(rootDir + ' has ' + str(count) + ' files')

listfiles('/home/dell/dataset/dresden', 'Dresden.txt')
D = Download()
D.run(debug=True)
